<?php

namespace app\admin\model;

use app\common\model\Tags;
use think\Model;

/**
 * Houses
 */
class Dealcase extends Model
{
    // 表名
    protected $name = 'dealcases';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;

    // 追加属性
    protected $append = [
        'city_text',
        'status_text',
        'category_text',
        'address_text',
        'surround_text',
        'property_text',
        'industry_text',
        'seal_status_text',
        'paymethod_text',
        'domain_images'
    ];

    public function getDomainImagesAttr($value, $data)
    {
        $domain = request()->domain();
        $images = $data['images'];
        if (empty($images)) return '';
        $images = explode(',', $images);
        $domain_images = [];
        foreach ($images as $image) {
            $domain_images[] = $domain . $image;
        }
        return $domain_images;
    }

    public function getPaymethodTextAttr($value, $data)
    {
        $seal_status = [1 => "押一付一", "3" => "押一付三", "10" => "押一付十", "6" => "半年付", "12" => "年付"];
        return isset($seal_status[$data['paymethod']])? $seal_status[$data['paymethod']] : '_';
    }

    public function getStatusTextAttr($value, $data)
    {
        $seal_status = [0 => '已下架', 1 => '已上架', 2 => '待审核', 3 => '已驳回'];
        return isset($seal_status[$data['status']])? $seal_status[$data['status']] :'-';
    }

    public function getSealStatusTextAttr($value, $data)
    {
        $seal_status = [0 => '未出售', 1 => '已售出'];
        return $seal_status[$data['seal_status']];
    }

    public function getCategoryTextAttr($value, $data)
    {
        return (new Category())->whereIn('id', $data['category_ids'])->column('name');
    }

    public function getAddressTextAttr($value, $data)
    {
        if ($data['city'] === '' || $data['city'] === null) return '';
        $cityNames = \think\facade\Db::name('area')->whereIn('id', $data['city'])->where(['level' => 3])->value('name');
        $address = $data['address'];
        return $cityNames ? $cityNames . '/' . $address : $address;
    }

    protected static function onAfterInsert($model)
    {
        if ($model->weigh == 0) {
            $pk = $model->getPk();
            if (strlen($model[$pk]) >= 19) {
                $model->where($pk, $model[$pk])->update(['weigh' => $model->count()]);
            } else {
                $model->where($pk, $model[$pk])->update(['weigh' => $model[$pk]]);
            }
        }
    }

    public function getCategoryIdsAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setCategoryIdsAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getCityAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setCityAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }


    public function getCityTextAttr($value, $row): string
    {
        if ($row['city'] === '' || $row['city'] === null) return '';
        $cityNames = \think\facade\Db::name('area')->whereIn('id', $row['city'])->column('name');
        return $cityNames ? implode(',', $cityNames) : '';
    }

    public function getImagesAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setImagesAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getFilesAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setFilesAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }


    public function getPropertyAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setPropertyAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function setIndustryAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getIndustryAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function getSurroundAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setSurroundAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }


    public function getSurroundTextAttr($value, $row): array
    {
        return (new Tags())->whereIn('id', $row['surround'])->column('string');
    }

    public function getIndustryTextAttr($value, $row): array
    {
        return (new Tags())->whereIn('id', $row['industry'])->column('string');
    }

    public function getPropertyTextAttr($value, $row): array
    {
        return (new Tags())->whereIn('id', $row['property'])->column('string,image');
    }

    public function user(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\common\model\User::class, 'user_id', 'id');
    }
}
